package com.xinyue.auto.reader.process;

import java.util.List;

import org.dom4j.Element;

import com.xinyue.auto.annotation.StringReset;
import com.xinyue.auto.exception.OrderException;
import com.xinyue.auto.reader.Process;
import com.xinyue.auto.reader.ProcessContext;
import com.xinyue.auto.reader.util.OrderUtil;

public class OrderProcess extends ProcessContext{
	public OrderProcess(Process read) {
		super(read);
	}
	
	//order by子句
	@StringReset("")
	private String orderBy;

	@SuppressWarnings("unchecked")
	@Override
	protected void validate() throws Exception {
		Element order = xml.element(order_by_);
		
		if(order != null) {
			List<Element> os = order.elements(order_column_);
			if(os == null || os.size() == 0) {
				throw new OrderException("必须定义order-column子标签", holder.getException());
			}
			
			try {
				orderBy = OrderUtil.validate(os, xml);
			} catch (Exception e) {
				throw new OrderException(e.getMessage(), holder.getException());
			}
		}
	}

	@Override
	protected void install() {
		if(orderBy.length() > 0) {
			holder.getEntity().setOrderBy(orderBy);
		}
	}
}